package com.hua.yuan.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hua.yuan.base.Constents;
import com.hua.yuan.common.BaseActivity;
import com.hua.yuan.R;
import com.hua.yuan.manager.user.UserManagerModule;
import com.hua.yuan.manager.user.business.callback.IUserCallback;
import com.hua.yuan.provider.user.IUserProvider;
import com.hua.yuan.provider.user.User;
import com.hua.yuan.ui.data.LoginDataRequest;
import com.hua.yuan.userlogin.UserLoginModule;
import com.hua.yuan.util.ToastUtil;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;

import static com.hua.yuan.ui.RegisterActivity.CODE_TIME;

/**
 * Created by huangyang on 2018/5/9.
 * 忘记密码界面
 */

public class ForgetPwdActivity extends BaseActivity implements View.OnClickListener,IUserCallback<IUserProvider> {

    private ImageView backImage;
    private TextView titleView;

    private EditText forgetPwdPhone;
    private TextView forgetPwdGetCode;
    private EditText forgetPwdInputCode;
    private EditText forgetNewPwd;
    private EditText forgetCofPwd;
    private TextView forgetPwdLogin;

    private RelativeLayout loginLoadView;


    private int codeNum = 0;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);

        initView();
        initData();

    }

    private void initView() {


        backImage = findViewById(R.id.backImage);
        titleView = findViewById(R.id.titleView);

        forgetPwdPhone = findViewById(R.id.forgetPwdPhone);
        forgetPwdGetCode = findViewById(R.id.forgetPwdGetCode);
        forgetPwdInputCode = findViewById(R.id.forgetPwdInputCode);
        forgetNewPwd = findViewById(R.id.forgetNewPwd);
        forgetCofPwd = findViewById(R.id.forgetCofPwd);
        forgetPwdLogin = findViewById(R.id.forgetPwdLogin);

        loginLoadView = findViewById(R.id.loginLoadView);


    }

    private void initData() {

        backImage.setOnClickListener(this);
        titleView.setText("忘记密码");
        forgetPwdGetCode.setOnClickListener(this);
        forgetPwdLogin.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.backImage:
                finish();
                break;
            case R.id.forgetPwdGetCode:
                //获取验证码
                String msg = forgetPwdGetCode.getText().toString().trim();
                if ("发送验证码".equals(msg)) {
                    getCodeFun();
                }

                break;
            case R.id.forgetPwdLogin:
                //登录
                changedPwdFun();
                break;
        }

    }


    /**
     * 修改密码
     */
    private void changedPwdFun() {

        final String phone = forgetPwdPhone.getText().toString().trim();
        String code = forgetPwdInputCode.getText().toString().trim();
        String newPwd = forgetNewPwd.getText().toString().trim();
        final String cofPwd = forgetCofPwd.getText().toString().trim();

        if (phone.length() == 0) {
            ToastUtil.showShortToast("手机号不能为空");
            return;
        }
        if (phone.length() != 11) {
            ToastUtil.showShortToast("请输入正确的手机号");
            return;
        }

        if (code.length() == 0) {
            ToastUtil.showShortToast("请先获取验证码");
            return;
        }

        if (newPwd.length() == 0) {
            ToastUtil.showShortToast("请设置密码");
            return;
        }

        if (newPwd.length() < 6) {
            ToastUtil.showShortToast("密码不能少于6位");
            return;
        }

        if (!newPwd.equals(cofPwd)) {
            ToastUtil.showShortToast("两次输入的密码不一致");
            return;
        }
        loginLoadView.setVisibility(View.VISIBLE);
        Map userMap=new HashMap<>();
        userMap.put(UserManagerModule.MOBILE,phone);
        userMap.put(UserManagerModule.PASSWORD,newPwd);
        userMap.put(UserManagerModule.SMS_CODE,code);
        IUserProvider provider=UserLoginModule.getInstance().getProvider();
        UserManagerModule.getInstance().getUserManager().forgetPasswd(userMap,provider,this);

    }


    @SuppressLint("SetTextI18n")
    private void getCodeFun() {

        String phone = forgetPwdPhone.getText().toString().trim();
        if (phone.length() == 0) {
            ToastUtil.showShortToast("手机号不能为空");
            return;
        }
        if (phone.length() != 11) {
            ToastUtil.showShortToast("请输入正确的手机号");
            return;
        }

        codeNum = CODE_TIME;

        forgetPwdGetCode.setText("还剩" + codeNum + "秒");
        forgetPwdGetCode.setBackgroundResource(R.drawable.log_get_code_1_shape);

        handler.sendEmptyMessageDelayed(1, 1000);


        LoginDataRequest.getMsgCode(phone, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
            }
        });
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:

                    if (codeNum == 0) {


                        if (forgetPwdGetCode != null) {
                            forgetPwdGetCode.setBackgroundResource(R.drawable.log_bg_get_code);
                            forgetPwdGetCode.setText("发送验证码");
                        }

                    } else {
                        codeNum = codeNum - 1;
                        if (forgetPwdGetCode != null) {
                            forgetPwdGetCode.setBackgroundResource(R.drawable.log_get_code_1_shape);
                            forgetPwdGetCode.setText("还剩" + codeNum + "秒");
                        }
                        sendEmptyMessageDelayed(1, 1000);
                    }


                    break;
            }

        }
    };

    @Override
    public void onResult(@NonNull final IUserProvider pUserInfo) {

        if(TextUtils.isEmpty(pUserInfo.getPassword())){
            ToastUtil.showShortToast( "找回密码失败");
            return;
        }
        ToastUtil.showShortToast( "设置密码成功");
        if(!isFinishing()){
            finish();
        }
    }

    @Override
    public void onError(final Throwable e) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showShortToast( "找回密码失败");
            }
        });
    }
}
